package com.xb.sourest;

import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import butterknife.BindView;

import android.Manifest;
import android.os.Bundle;
import android.view.MenuItem;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.xb.sourest.base.BaseActivity;
import com.xb.sourest.ui.CarFragment;
import com.xb.sourest.ui.HomeFragment;
import com.xb.sourest.ui.MessageFragment;
import com.xb.sourest.ui.MineFragment;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class MainActivity extends BaseActivity {

    @BindView(R.id.bottom_navigation)
    BottomNavigationView bottomNavigationView;
    @BindView(R.id.viewpager)
    ViewPager2 viewpager;
    private List<Fragment> fragments;

    @Override
    protected int initLayoutRes() {
        return R.layout.activity_main;
    }

    @Override
    protected void loadData() {

    }

    @Override
    protected void upView() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new CarFragment());
        fragments.add(new MessageFragment());
        fragments.add(new MineFragment());


        bottomNavigationView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
        viewpager.setOffscreenPageLimit(ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT);
        viewpager.setAdapter(new FragmentStateAdapter(getSupportFragmentManager(), getLifecycle()) {
            @NonNull
            @Override
            public Fragment createFragment(int position) {
                return fragments.get(position);
            }

            @Override
            public int getItemCount() {
                return fragments.size();
            }
        });
        //页面改变监听
        viewpager.registerOnPageChangeCallback(changeCallback);
        viewpager.setUserInputEnabled(false);
    }

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            switch (menuItem.getItemId()) {
                case R.id.home_page:
                    viewpager.setCurrentItem(0, false);
                    return true;
                case R.id.car_page:
                    viewpager.setCurrentItem(1, false);
                    return true;
                case R.id.message_page:
                    viewpager.setCurrentItem(2, false);
                    return true;
                case R.id.mine_page:
                    viewpager.setCurrentItem(3, false);
                    return true;
            }
            return false;
        }
    };

    //页面改变监听
    private ViewPager2.OnPageChangeCallback changeCallback = new ViewPager2.OnPageChangeCallback() {
        @Override
        public void onPageSelected(int position) {
            bottomNavigationView.getMenu().getItem(position).setChecked(true);
        }

    };

    @Override
    protected void onDestroy() {
        //销毁前调用
        viewpager.unregisterOnPageChangeCallback(changeCallback);
        super.onDestroy();
    }
}